﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using Web.Data;

namespace Web.Service
{
    public partial class WebService
    {
        /// <summary>
        /// 通过餐厅Id获取菜单列表
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [WebMethod]
        public List<Data.ViewMenu> GetMenuByDiningRoomId(int id)
        {
            Data.DataClassesDataContext c = new Data.DataClassesDataContext();

            var list = c.ViewMenu.Where(m => m.DiningRoomId == id).ToList();

            return list;
        }

        /// <summary>
        /// 获取所有餐厅列表
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [WebMethod]
        public List<DiningRoom> GetDiningRoom()
        {
            Data.DataClassesDataContext d = new DataClassesDataContext();

            var b = d.DiningRoom.Where(p => p.Id>0).ToList();

            return b;
        }

        /// <summary>
        /// 获取推荐菜单列表
        /// </summary>
        /// <returns></returns>
        [WebMethod]
        public List<Menu> GetMenuIs_TopTrue()
        {
            Data.DataClassesDataContext e = new DataClassesDataContext();

            var f = e.Menu.Where(p=>p.Is_top == true).ToList();

            return f;
        }

        /// <summary>
        /// 通过菜单Id获取菜单
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [WebMethod]
        public Menu GetImgByMenuId(int id)
        {
            Data.DataClassesDataContext x = new DataClassesDataContext();

            var y = x.Menu.Where(p => p.Id == id).SingleOrDefault();

            return y;
        }

        /// <summary>
        /// 插入一条订单详情
        /// </summary>
        /// <param name="oderdetail"></param>
        [WebMethod]
        public void InsertOrderDetail(OrderDetail oderdetail)
        {
            Data.DataClassesDataContext p = new DataClassesDataContext();

            p.OrderDetail.InsertOnSubmit(oderdetail);

            p.SubmitChanges();
        }

        /// <summary>
        /// 通过OrderId获取订单详情列表
        /// </summary>
        /// <param name="addtime"></param>
        /// <returns></returns>
        [WebMethod]
        public List<ViewOrderDetail> GetOrderDetailByOrderId(int orderId)
        {
            Data.DataClassesDataContext z = new DataClassesDataContext();

            var y = z.ViewOrderDetail.Where(p => p.OrderId == orderId).OrderBy(p=>p.AddTime).ToList();

            return y;
        }


        /// <summary>
        /// 通过某天获取订单列表
        /// </summary>
        /// <param name="addtime"></param>
        /// <returns></returns>
        [WebMethod]
        public List<Order> GetOrderByYearOfDay(int yearofday)
        {
            Data.DataClassesDataContext z = new DataClassesDataContext();

            var y = z.Order.Where(p => p.AddTime.DayOfYear == yearofday).OrderByDescending(p=>p.AddTime).ToList();

            return y;
        }

        /// <summary>
        /// 插入一条订单
        /// </summary>
        /// <param name="order"></param>
        [WebMethod(EnableSession = true)]
        public void InsetOrder(Order order)
        {
            if (Context.Session["UserId"] != null && Context.Session["UserId"].ToString() != "")
            {
                Data.DataClassesDataContext context = new DataClassesDataContext();

                int userid = int.Parse(Context.Session["UserId"].ToString());

                order.UserId = userid;

                context.Order.InsertOnSubmit(order);

                context.SubmitChanges();
            }
            else
            {
                throw new Exception("非法操作");
            }
        }

        /// <summary>
        /// 插入一条用户数据
        /// </summary>
        /// <param name="user"></param>
        [WebMethod]
        public void InsertUser(User user)
        {
            Data.DataClassesDataContext u = new DataClassesDataContext();

            u.User.InsertOnSubmit(user);

            u.SubmitChanges();
        }

        /// <summary>
        /// 判断用户是否登录成功
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        [WebMethod(EnableSession = true)]
        public bool LoginSystem(string name, string pwd)
        {
            Data.DataClassesDataContext context = new DataClassesDataContext();

            var q = context.User.Where(p => p.Name == name & p.Pwd == pwd).SingleOrDefault();

            if (q != null && q.Id > 0)
            {
                Context.Session["UserId"] = q.Id;
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 判断用户是否登录
        /// </summary>
        /// <returns></returns>
        [WebMethod(EnableSession = true)]
        public bool IsLoginSystem()
        {
            if (Context.Session["UserId"] != null && Context.Session["UserId"].ToString() != "")
            {
                return true;
            }
            else
            {
                return false;
            }
        }

    }
}